package com.zt.teacher.controller;

import com.zt.common.pojo.common.JsonResult;
import com.zt.common.pojo.homework.Homework;
import com.zt.common.pojo.homework.HomeworkAnswer;
import com.zt.teacher.dto.HomeworkInfoDTO;
import com.zt.teacher.dto.HomeworkWithClassDTO;
import com.zt.teacher.dto.SummaryDTO;
import com.zt.teacher.service.HomeworkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2022-02-10
 */
@RestController
@CrossOrigin
@RequestMapping("homework")
@Api("作业模块")
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;

    @PostMapping("add")
    @ApiOperation("新增作业")
    public JsonResult<Integer> addHomeWork(@RequestBody Homework homework){
        int homeworkId = homeworkService.insert(homework);
        return JsonResult.success(homeworkId);
    }

    @GetMapping("getByClassId/{classId}")
    @ApiOperation("根据班级id获取作业及提交、批改概况")
    public JsonResult<List<HomeworkInfoDTO>> getHomeworkAndAnswer(HttpServletRequest request, @PathVariable int classId){
        return JsonResult.success(homeworkService.getHomeworkAndAnswer(request,classId));
    }

    @GetMapping("getBySemester/{semester}")
    @ApiOperation("根据学期获取该老师所有课堂以及作业情况")
    public JsonResult<List<HomeworkWithClassDTO>> getByTeacherId(HttpServletRequest request, @PathVariable String semester){
        List<HomeworkWithClassDTO> homeworks = homeworkService.getBySemester(request,semester);
        return JsonResult.success(homeworks);
    }

    @GetMapping("getByHomeworkId/{homeworkId}")
    @ApiOperation("获取单个作业以及回答情况")
    public JsonResult<Homework> getByHomeworkId(HttpServletRequest request, @PathVariable int homeworkId){
        return JsonResult.success(homeworkService.getByHomeworkId(request,homeworkId));
    }

    @DeleteMapping("delete/{homeworkId}")
    @ApiOperation("根据作业id删除作业")
    public JsonResult delete(HttpServletRequest request, @PathVariable int homeworkId){
        homeworkService.delete(request,homeworkId);
        return JsonResult.success();
    }

    @PostMapping("mark")
    @ApiOperation("给作业回答评分")
    public JsonResult markHomework(HttpServletRequest request, @RequestBody HomeworkAnswer homeworkAnswer){
        homeworkService.markHomework(request,homeworkAnswer);
        return JsonResult.success();
    }

    @GetMapping("getSummary/{classId}")
    @ApiOperation("获取该班级的所有考试平均分")
    public JsonResult<List<SummaryDTO>> getSummaryByClassId(@PathVariable int classId){
        List<SummaryDTO> homeworkSummary = homeworkService.getSummaryByClassId(classId);
        return JsonResult.success(homeworkSummary);
    }
}
